import { queryCustomerBanks, getCustomerByNameOrCode, createBankSubAccount, applyConversion, updateBankSubAccount, getBankSubAccount} from '../../services/customer/customer';
import Constants from '../../utils/Constants';
import { message } from "antd";
const R = require('ramda');

export default {
	namespace: 'customerBank',
	state: {
		page: Constants.DEFAULTPAGE,
		//银企直联列表
		list: [],
		currentCustomer: {},
		dataSource: {}
	},
	reducers: {
        /**
         * 刷新state
         * @param state
         * @param payload
         */
		refreshState(state, { payload }) {
			return { ...state, ...payload }
		},
	},
	effects: {
        /**
         * 分页查询银企直联数据列表
         *
         * @param call
         * @param put
         * @param select
         */
		*queryBankCustomers({ payload }, { call, put, select }) {
			let page = yield select(state => state.customerBank.page);
			page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;
			const res = yield call(queryCustomerBanks, { ...payload });
			if (res.data.resultCode === "ok") {
				let content = res.data.content;
				yield put({
					type: 'refreshState',
					payload: {
						list: content.list ? content.list : [],
						page: content.page ? content.page : Constants.DEFAULTPAGE,
					}
				});
			} else {
				message.error(res.data.errMessage);
			}
		},

		/**
         * 校验客户
         *
         * @param call
         * @param put
         * @param select
         */
		*getCustomerByNameOrCode({ payload }, { call, put, select }) {
			const res = yield call(getCustomerByNameOrCode, { ...payload });
			if (res.data.resultCode === "ok") {
				let content = res.data.content;
				if(!content || content.length === 0){
					message.error("客户匹配失败，请重新输入客户编号");
				}else{
					if(content.length > 1){
						message.error("客户匹配过多，请输入精确的客户编号");
					}else{
						if(payload.onSuccess)payload.onSuccess(content[0]);
					}
				}
			} else {
				message.error(res.data.errMessage);
			}
		},

		*createBankSubAccount({ payload }, { call, put, select }) {
			const res = yield call(createBankSubAccount, {...R.omit(['onSuccess', 'onFail'])(payload)});
			if (res.data.resultCode === "ok") {
				message.info("银企直联子账号创建成功!");
				if(payload.onSuccess) payload.onSuccess();
			} else {
				message.error(res.data.errMessage);
			}
		},

		*updateBankSubAccount({ payload }, { call, put, select }) {
			const res = yield call(updateBankSubAccount, {...R.omit(['onSuccess', 'onFail'])(payload)});
			if (res.data.resultCode === "ok") {
				message.info("银企直联子账号修改成功!");
				if(payload.onSuccess) payload.onSuccess();
			} else {
				message.error(res.data.errMessage);
			}
		},

		*getBankSubAccount({ payload }, { call, put, select }) {
			if(payload && payload.id){
				const res = yield call(getBankSubAccount, {...R.omit(['onSuccess', 'onFail'])(payload)});
				if (res.data.resultCode === "ok") {
					yield put({
						type: 'refreshState',
						payload: {
							dataSource: res.data.content
						}
					});
					if(payload.onSuccess) payload.onSuccess();
				} else {
					message.error(res.data.errMessage);
				}
			}else{
				yield put({
					type: 'refreshState',
					payload: {
						dataSource: {}
					}
				});
				if(payload.onSuccess) payload.onSuccess();
			}
		},

		*applyConversion({ payload }, { call, put, select }) {
			const res = yield call(applyConversion, { ...payload });
			if (res.data.resultCode === "ok") {
				message.info("结汇成功，已认领到对应客户，请尽快补充水单并复核");
				if(payload.onSuccess) payload.onSuccess();
			} else {
				message.error(res.data.errMessage);
			}
		}
	}
};